package com.frcdi.qms.masterdata.service.impl;

import com.frcdi.qms.common.constant.UserConstants;
import com.frcdi.qms.common.utils.DateUtils;
import com.frcdi.qms.common.utils.SecurityUtils;
import com.frcdi.qms.common.utils.StringUtils;
import com.frcdi.qms.masterdata.domain.ChangeExecution;
import com.frcdi.qms.masterdata.domain.CodeConfig;
import com.frcdi.qms.masterdata.domain.Method;
import com.frcdi.qms.masterdata.mapper.ChangeExecutionMapper;
import com.frcdi.qms.masterdata.mapper.CodeConfigMapper;
import com.frcdi.qms.masterdata.mapper.MethodMapper;
import com.frcdi.qms.masterdata.service.IMethodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 方法Service业务层处理
 *
 * @author cell
 * @date 2023-10-16
 */
@Service
public class MethodServiceImpl implements IMethodService
{
    @Autowired
    private MethodMapper methodMapper;

    @Autowired
    private CodeConfigMapper codeMapper;

    @Autowired
    private ChangeExecutionMapper executionMapper;

    /**
     * 查询方法
     *
     * @param methodId 方法主键
     * @return 方法
     */
    @Override
    public Method selectMethodByMethodId(Long methodId)
    {
        return methodMapper.selectMethodByMethodId(methodId);
    }

    /**
     * 查询方法列表
     *
     * @param method 方法
     * @return 方法
     */
    @Override
    public List<Method> selectMethodList(Method method)
    {
        return methodMapper.selectMethodList(method);
    }

    /**
     * 新增方法
     *
     * @param method 方法
     * @return 结果
     */
    @Override
    public String insertMethod(Method method)
    {
        methodMapper.insertMethod(method);
        return method.getMethodId().toString();
    }

    /**
     * 修改方法
     *
     * @param method 方法
     * @return 结果
     */
    @Override
    public int updateMethod(Method method)
    {
        return methodMapper.updateMethod(method);
    }
    @Override
    public int updateMethodByMethodNo(Method method)
    {
        return methodMapper.updateMethodByMethodNo(method);
    }

    /**
     * 批量删除方法
     *
     * @param methodIds 需要删除的方法主键
     * @return 结果
     */
    @Override
    public int deleteMethodByMethodIds(Long[] methodIds)
    {
        return methodMapper.deleteMethodByMethodIds(methodIds);
    }

    /**
     * 删除方法信息
     *
     * @param methodId 方法主键
     * @return 结果
     */
    @Override
    public int deleteMethodByMethodId(Long methodId)
    {
        return methodMapper.deleteMethodByMethodId(methodId);
    }

    /**
     * 检查名称是否唯一
     *
     * @param method 方法
     * @return 结果
     */
    @Override
    public boolean checkMethodNameUnique(Method method) {
        Long equipmentBalanceId = StringUtils.isNull(method.getMethodId()) ? -1L : method.getMethodId();
        Method info = methodMapper.checkMethodNameUnique(method.getMethodName(), method.getMethodType());
        if (StringUtils.isNotNull(info) && info.getMethodId().longValue() != equipmentBalanceId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Transactional
    @Override
    public int submitApplicant(Method method) {
        Method upMethod = new Method();
        upMethod.setMethodId(method.getMethodId());
        upMethod.setSend(UserConstants.SEND_HAS_RETURNED);
        methodMapper.updateMethod(upMethod);

        ChangeExecution execution = new ChangeExecution();
        execution.setChangeNo(method.getChangeNo());
        execution.setAssociatedId(method.getMethodId());
        execution.setAssociatedName(method.getMethodName());
        execution.setAssociatedType(UserConstants.METHOD);
        execution.setCreateBy(SecurityUtils.getUsername());
        return executionMapper.insertChangeExecution(execution);
    }

    @Override
    public String getCode() {
        String syscode = new StringBuilder().toString();
        // 获取前缀
        CodeConfig codeConfig = codeMapper.selectCodeConfigByCodeId(3L);
        if (StringUtils.equals("1",codeConfig.getCodeTime())){
            syscode = codeConfig.getCodeRule()+DateUtils.dateTime();
        }else {
            syscode = codeConfig.getCodeRule();
        }
        // 获取最近一次生成
        String code = methodMapper.getCode(syscode);

        if (StringUtils.isNull(code)){
            return syscode + StringUtils.padl(1,codeConfig.getCodeNumber());
        }

        String newcode = StringUtils.substring(code, code.length() - codeConfig.getCodeNumber(), code.length());
        return syscode + (StringUtils.padl(Integer.valueOf(newcode)+1,codeConfig.getCodeNumber()));
    }
}
